#!/usr/bin/env python
#coding=utf-8

from __future__ import division
import time
import sys
from define import BASE_DIR,BLACK_LIST
sys.path.append(BASE_DIR)

#import pymongo
from lib.abstract_cron import abstract_cron
from lib.func_ext import str2sec,sec2str,ksort

#需要修改
class order_day(abstract_cron) :

    #需要修改
    #noLock = True   #是否生成脚本锁文件
    insertTable = 'sdb_statistics_order_day'

    def __init__(self) :
        #需要修改
        super(order_day,self).__init__(__file__)
        self.mongo = self.objCore.mongo('mongodb_stat')
        dbConf = self.setting.get('mongodb_stat',{})
        mongoDB = dbConf.get('mongo_db_stat')
        mongoOrderColl = dbConf.get('mongo_order_coll')
        self.orderColl = self.mongo[mongoDB][mongoOrderColl]

    def boot(self,timestamp=[]) :
        #self.preBoot()

        keyFilter = self.getTimeFilter(timestamp)
        objCursor = self.orderColl.find(keyFilter)

        if objCursor :
            for v in objCursor :
                v['calltime'] = str2sec(int(v.get('date')),'%Y%m%d')
                #数据过滤
                self.filterItems(v)

            #批量插入
            self.doBatchExec()

        self.sufBoot()
        print 'ok'

    def getTimeFilter(self,timestamp=[]) :
        global BLACK_LIST
        keyFilter = {}
        keyFilter['date'] = timestamp and timestamp or (int(sec2str('%Y%m%d',time.time()-1*24*3600)))
        keyFilter['node_id'] = {'$nin':BLACK_LIST}
        return keyFilter

    def insertKeys(self) :
        return ['node_id','node_type','num','price','date','calltime']


#需要修改
if __name__ == '__main__' :
    obj = order_day()
    obj.boot()
    #obj.boot(20110314)
